package com.tuanzi.loan.web.bean.collaborate;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelRatio;
import com.tuanzi.loan.business.service.collaborate.CollaborateChannelRatioService;
import com.tuanzi.loan.business.service.collaborate.CollaborateChannelUserService;
import com.tuanzi.loan.vo.response.collaborate.CollaborateConfigListResponse;
import com.tuanzi.loan.web.bean.CurrentUserBean;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 16:27 2018/9/18
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaborateConfigInfoBean extends JSFInfoBean<CollaborateConfigListResponse>{
    private static final long serialVersionUID = 1676062256903126221L;

    @Autowired
    private CollaborateChannelUserService collaborateChannelUserService;
    @Autowired
    private CollaborateChannelRatioService collaborateChannelRatioService;
    @Autowired
    private CurrentUserBean currentUserBean;

    private String[] marketArray;
    private String[] checkArray;


    @PostConstruct
    private void init(){
        String idStr = getParamFromRequest("id");
        int id = NumberUtils.toInt(idStr);

        if(id == 0){
            $ = new CollaborateConfigListResponse();

        }else{
            $ =  collaborateChannelUserService.findCollaborateConfigInfo(id);
            if(Objects.isNull($)){
                forceRedirect("/views/collaborate/collaborate-config-list.xhtml");
                return;
            }

            initData($);
            changeStatusToEdit();
        }
    }

    private void initData(CollaborateConfigListResponse response){
        String checkShow = response.getCheckShow();
        String marketShow = response.getMarketShow();
        if(StringUtils.isNotBlank(checkShow)){
            checkArray = checkShow.split(",");
        }
        if(StringUtils.isNotBlank(marketShow)){
            marketArray = marketShow.split(",");
        }
    }

    public void channelChange(){
        Integer channelId = $.getChannelId();
        if(channelId != null && 9999 != channelId){
            NewCollaborateChannelRatio ratioByChannelId = collaborateChannelRatioService.findCollaborateChannelRatioByChannelId(channelId);
            if(Objects.nonNull(ratioByChannelId)){
                $.setTomorrowRatio(ratioByChannelId.getTomorrowRatio());
                $.setShowType(ratioByChannelId.getShowType());
            }
        }

    }

    public void save(){
        if(StringUtils.isBlank($.getAccount())){
            addWarnMessage("合作方账户不能为空");
            return;
        }
        if(9999 == $.getChannelId()){
            addWarnMessage("渠到不能选择通用渠道");
            return;
        }

        if($.getTomorrowRatio() == null){
            addWarnMessage("请配置合作方系数");
            return;
        }

        if($.getProductId() == null){
            addWarnMessage("请配置父产品");
            return;
        }

        if(marketArray.length >0){
            String marketShow = StringUtils.join(marketArray, ",");
            $.setMarketShow(marketShow);
        }
        if(checkArray.length > 0){
            String checkShow = StringUtils.join(checkArray, ",");
            $.setCheckShow(checkShow);
        }

        collaborateChannelUserService.saveUserChannelAndRatio($);

        forceRedirect("/views/collaborate/collaborate-config-list.xhtml");
    }

    public String back(){
        return redirect(currentUserBean.getBack());
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.COLLABORATE_TABLE_SHOW_TYPE,DictionaryCode.PARENT_PRODUCT,
                DictionaryCode.COLLABORATE_MARKET_SHOW_TYPE, DictionaryCode.COLLABORATE_CHECK_SHOW_TYPE);
    }
}
